<?php
$servername = "47.107.117.221";
$username = "root_s";
$password = "r4wHMmMe8CRafnBp";
$dbname = "info";
// 创建连接
$conn = mysqli_connect($servername, $username, $password, $dbname);
// 检测连接
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}

if ($_POST['username'] == "" || $_POST['password'] == "" || $_POST['mail'] == "") {
    echo "<script>alert('用户名/密码/邮箱/不能为空');location.href='https://m.smitish.com/yiban'</script>";
    die();
    // echo  "用户名/密码/邮箱/不能为空";
}


$sql = "SELECT * FROM users_yiban WHERE  username='" . $_POST['username'] . "' AND password='" . $_POST['password'] . "'";
// $sql = "SELECT * FROM users_yiban WHERE username = '18990339825'";
$result = $conn->query($sql);
//$dangqianweizhi = $_POST['shen'] . "/" . $_POST['shi'] . "/" . $_POST['qu'];
if ($_POST['waichuguiji'] == "")
    $waichuguiji = "null";
else
    $waichuguiji = $_POST['waichuguiji'];

if ($result->num_rows > 0) {
    // 输出数据
    // echo  $_POST["username"] . "已存在数据库";
    $updateSql = "UPDATE `users_yiban` SET `sushe`='" . $_POST['sushe'] . "', `dangqianweizhi`='" . $_POST['shen'] . "/" . $_POST['shi'] . "/" . $_POST['qu'] . "',`dangqianzaixiao`= '" . $_POST['dangqianzaixiao'] . "',`jinrichuxiao` = '" . $_POST['jinrichuxiao'] . "', `shenghuoguiji`= '" . $_POST['shenghuoguiji'] . "', `waichuguiji` = '$waichuguiji',`xingqi`= '2',mail =   '" . $_POST['mail'] . "'WHERE  `username`='" . $_POST['username'] . "' AND `password`='" . $_POST['password'] . "'";
    // echo $updateSql;
    // echo mysqli_query($conn, $updateSql);
    if (mysqli_query($conn, $updateSql))
        echo "<script>alert('修改[" . $_POST['username'] . "]数据成功.');location.href='https://m.smitish.com/yiban'</script>";
    die();
} else {
    $sql = "SELECT * FROM users_yiban WHERE  username='" . $_POST['username'] . "'";
    $results = $conn->query($sql);
    if ($_POST['shen'] == "" || $_POST['shi'] == "" || $_POST['qu'] == "") {
        // echo  "当前位置有误";
        echo "<script>alert('当前位置必须输入');location.href='https://m.smitish.com/yiban'</script>";
        die();
    } else if ($results->num_rows > 0) {
        // 存在用户名
        echo "<script>alert('[" . $_POST['username'] . "]已存在于数据库.');location.href='https://m.smitish.com/yiban'</script>";
        die();
    } else {
        $sql = "INSERT INTO users_yiban (username, password, sushe,dangqianweizhi,dangqianzaixiao,jinrichuxiao,shenghuoguiji,waichuguiji,xingqi,mail)
VALUES ('" . $_POST['username'] . "', '" . $_POST['password'] . "',' " . $_POST['sushe'] . "','" . $_POST['shen'] . "/" . $_POST['shi'] . "/" . $_POST['qu'] . "','" . $_POST['dangqianzaixiao'] . "','" . $_POST['jinrichuxiao'] . "','" . $_POST['shenghuoguiji'] . "', '$waichuguiji' ,'2','" . $_POST['mail'] . "')";
        if ($conn->query($sql) === TRUE) {
            echo "<script>alert('提交成功');location.href='https://m.smitish.com/yiban'</script>";
        } else {
            echo "<script>alert('提交错误');location.href='https://m.smitish.com/yiban'</script>";
        }
    }
}

mysqli_close($conn);

// if (mysqli_query($conn, $sql)) {
//     echo "<script>alert('提交成功');location.href='https://form.smitish.com'</script>";
// } else {
//     echo "<script>alert('提交失败,请重新填写');location.href='https://form.smitish.com'</script>";
// }
